package pack;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Network {

	private static Network instance = null;
	private static HashMap<String, String> aps = new HashMap<String, String>();
	private static String file = "aps/aps.tsv";

	private Network() {
	}

	// @ ensures (\instance)!=null;
	public static Network getInstance() {
		if (instance == null) {
			instance = new Network();
			try {
				instance.loadNet(file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return instance;
	}

	// Loads file and create an ArrayList of aps

	// @ requires file_path != null;
	private/* static */void loadNet(String file_path) throws IOException {
		try (DataInputStream file = new DataInputStream(
				new BufferedInputStream(
						new FileInputStream(new File(file_path))))) {
			String[] line;

			while (file.available() > 0) {
				line = file.readLine().split("\t");
				aps.put(line[1], line[0]);
			}

			System.out.println("APs list loaded");
		}
	}

	// Searchs for ap.name based on mac code

	// @ requires mac != null;
	// @ ensures \response != null;
	public/* static */String search(String mac) {
		// String response;
		if (aps.isEmpty()) {
			try {
				loadNet(file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return aps.get(mac);
	}

}
